package com.jiankangjin.alipaychannel.controller;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.alipaychannel.services.impl.PayServiceImpl;
import com.jiankangjin.alipaychannel.util.StringUtil;
import com.jiankangjin.alipaychannel.vo.PayVo;

/**
 * 统一支付渠道-支付接口
 */
@Controller
public class PayController {
	private static final transient Log logger = LogFactory.getLog(PayController.class);
	
	/**
	 * createAndPay 创建并支付订单
	 */
	@RequestMapping(value="/pay", method=RequestMethod.POST)
	@ResponseBody
	public PayVo createAndPay (
			@RequestParam(required = false) String channel_fee_type,
			@RequestParam(required = false) String version,
			@RequestParam(required = false) String pay_ssn_id,
			@RequestParam(required = false) String order_id,
			@RequestParam(required = false) String pay_fee,
			@RequestParam(required = false) String pay_cry,
			@RequestParam(required = false) String buyer_token,
			@RequestParam(required = false) String buyer_voucher,
			@RequestParam(required = false) String subjet,
			@RequestParam(required = false) String remark,
			@RequestParam(required = false) String buyer_name,
			@RequestParam(required = false) String buyer_email,
			@RequestParam(required = false) String seller_name,
			@RequestParam(required = false) String seller_email,
			@RequestParam(required = false) String expand_json,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("channel_fee_type=" + channel_fee_type);
		sb.append(" version=" + version);
		sb.append(" pay_ssn_id=" + pay_ssn_id);
		sb.append(" order_id=" + order_id);
		sb.append(" pay_fee=" + pay_fee);
		sb.append(" pay_cry=" + pay_cry);
		sb.append(" buyer_token=" + buyer_token);
		sb.append(" buyer_voucher=" + buyer_voucher);
		sb.append(" subjet=" + subjet);
		sb.append(" remark=" + remark);
		sb.append(" buyer_name=" + buyer_name);
		sb.append(" buyer_email=" + buyer_email);
		sb.append(" seller_name=" + seller_name);
		sb.append(" seller_email=" + seller_email);
		sb.append(" expand_json=" + expand_json);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("支付接口：" + sb);
		}
		//验证有效性
		PayVo payVo = new PayVo();
		if (StringUtil.isEmpty(channel_fee_type)) {
			payVo.setResp_msg("channel_fee_type不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(version)) {
			payVo.setResp_msg("version不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(pay_ssn_id)) {
			payVo.setResp_msg("pay_ssn_id不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(order_id)) {
			payVo.setResp_msg("order_id不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(pay_fee)) {
			payVo.setResp_msg("pay_fee不能为空");
			return payVo;
		} else {
			if (StringUtil.isNotNum(pay_fee)) {
				payVo.setResp_msg("pay_fee必须为数字");
				return payVo;
			} else {
				if (Integer.parseInt(pay_fee) <= 0) {
					payVo.setResp_msg("pay_fee必须大于零");
					return payVo;
				}
			}
		}
		if (StringUtil.isEmpty(pay_cry)) {
			payVo.setResp_msg("pay_cry不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(buyer_token)) {
			payVo.setResp_msg("buyer_token不能为空");
			return payVo;
		}
		if (StringUtil.isEmpty(buyer_voucher)) {
			payVo.setResp_msg("buyer_voucher不能为空");
			return payVo;
		}
		if (!channel_fee_type.equalsIgnoreCase("public") && 
				!channel_fee_type.equalsIgnoreCase("barcode") && 
				!channel_fee_type.equalsIgnoreCase("default")) {
			payVo.setResp_msg("channel_fee_type必须为public barcode default 不区分大小写");
			return payVo;
		}
		if (!version.equalsIgnoreCase("V0.0.1") && 
				!version.equalsIgnoreCase("V0.1") && 
				!version.equalsIgnoreCase("V1")) {
			payVo.setResp_msg("version必须类似于0.0.1 V0.1 V1 不区分大小写");
			return payVo;
		}
		//验证签名
		
		//支付类型和版本路由
		try {
			String methodname = channel_fee_type + "_" +version.toUpperCase().replace(".", "_");
			Method transMethod = PayServiceImpl.class.getMethod(methodname, new Class[]{String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class,String.class});
			return (PayVo) transMethod.invoke(PayServiceImpl.class.newInstance(), pay_ssn_id,
					order_id, pay_fee, pay_cry, buyer_token, buyer_voucher,
					subjet, remark, buyer_name, buyer_email, seller_name,
					seller_email, expand_json);
		} catch (Exception e) {
			payVo.setResp_msg("支付类型和版本路由出错");
			e.printStackTrace();
		}
		return payVo;
	}
}